package service

import (
	"disk/disk_rpc_server/disk_pb"
	"disk/utils/etcd"
	"disk/utils/mysql"
	"fmt"
	"github.com/spf13/viper"
	clientv3 "go.etcd.io/etcd/client/v3"
	"gorm.io/gorm"
)

var Database *gorm.DB
var ETCD *clientv3.Client

type GrpcServer struct {
	disk_pb.UnimplementedDiskServiceServer
}

func Init() {
	var err error

	viper.AddConfigPath("./conf/")
	viper.SetConfigName("rpc_server_config")
	viper.SetConfigType("yaml")

	err = viper.ReadInConfig()
	if err != nil {
		panic(fmt.Sprintf("配置文件错误 %s", err.Error()))
	}

	Database, err = mysql.ConnectToDatabase("disk_db")
	if err != nil {
		fmt.Println("Error connecting to database:", err)
		return
	}
	err = Database.AutoMigrate(&mysql.DiskList{})
	if err != nil {
		fmt.Println("Error auto migrate disk list:", err)
		return
	}

	ETCD = etcd.NewEtcdClient("etcd")
}
